package name.as3.simpleUIComponents.controls.alertClasses
{
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextField;

import name.as3.simpleUIComponents.controls.SimpleUIButton;
import name.as3.simpleUIComponents.core.SimpleSprite;
import name.as3.simpleUIComponents.defaultSkins.DefaultSimpleAlertFormSkin;
import name.as3.simpleUIComponents.interfaces.skins.ISimpleAlertFormSkin;
import name.as3.simpleUIComponents.types.Size;

public class SimpleAlertForm extends SimpleSprite
{
	private var m_background:DisplayObject;
	private var m_btnOK:SimpleUIButton;
	private var m_btnCancel:SimpleUIButton;
	private var m_fnCallback:Function;
	private var m_skin:ISimpleAlertFormSkin;
	private var m_ttf:TextField;
	private var m_size:Size;
	/**
	 *
	 */
	public function SimpleAlertForm()
	{
		super();
		m_ttf = new TextField();
		m_ttf.mouseEnabled = false;
		this.skin = new DefaultSimpleAlertFormSkin();
	}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// event
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	private function click_btnOk(e:MouseEvent):void
	{
		if (parent)
		{
			parent.removeChild(this);
			_invokeCallback(true);
		}
	}
	private function click_btnCancel(e:MouseEvent):void
	{
		if (parent)
		{
			parent.removeChild(this);
			_invokeCallback(false);
		}
	}
	private function _invokeCallback(blOk:Boolean):void
	{
		if (m_fnCallback != null)
		{
			try
			{
				m_fnCallback(blOk);
				this.dispose();
			}
			catch(ex:Error)
			{
				trace(ex.toString());
			}
		}
	}
//==================================================================================================
// public
//==================================================================================================
	override public function dispose():void
	{
		m_fnCallback = null;
		m_btnOK.removeEventListener(MouseEvent.CLICK, click_btnOk);
		if (m_btnCancel)
		{
			m_btnCancel.removeEventListener(MouseEvent.CLICK, click_btnCancel);
		}
	}
	/**
	 *
	 */
	public function show(str:String, 
						 strTitle:String = null,
						 fnCallback:Function = null,
						 blCancel:Boolean = false):void
	{
		m_fnCallback = fnCallback;
		if (null == m_btnOK && m_skin)
		{
			m_ttf.text = str;
			m_btnOK = new SimpleUIButton();
			m_btnOK.text = m_skin.okButtonText;
			m_btnOK.y = this.height - m_skin.buttonBottonMargin;
			m_btnOK.addEventListener(MouseEvent.CLICK, click_btnOk);
			super.addChild(m_btnOK);
			if (blCancel)
			{
				m_btnCancel = new SimpleUIButton();
				m_btnCancel.text = m_skin.cancelButtonText;
				m_btnCancel.y = m_btnOK.y;
				m_btnOK.setSize(m_skin.okButtonSize4Cancel.width, m_skin.okButtonSize4Cancel.height);
				m_btnCancel.setSize(m_skin.cancelButtonSize.width, m_skin.cancelButtonSize.height);
				m_btnOK.x = 
					(this.width - m_btnOK.width - m_btnCancel.width - m_skin.buttonDistance) * .5;
				m_btnCancel.x = 
					m_btnOK.x + m_btnOK.width + m_skin.buttonDistance;
				m_btnCancel.addEventListener(MouseEvent.CLICK, click_btnCancel);
				super.addChild(m_btnCancel);
			}
			else
			{
				m_btnOK.setSize(m_skin.okButtonSize.width, m_skin.okButtonSize.height);
				m_btnOK.x = (this.width - m_btnOK.width) * .5;
			}
		}
	}
	/**
	 * 设置皮肤
	 */
	public function set skin(value:ISimpleAlertFormSkin):void
	{
		if (value)
		{
			m_skin = value;
			m_size = value.defaultSize;
			m_background = value.backgroundImage;
			m_ttf.x = value.textPosition.x;
			m_ttf.y = value.textPosition.y;
			m_ttf.width = 0;
			m_ttf.height = 0;
			m_ttf.defaultTextFormat = m_skin.defaultTextFormat;
			super.addChild(value.backgroundImage);
			super.addChild(m_ttf);
			this.setSize(m_size.width, m_size.height);
			m_ttf.width = this.width - m_ttf.x * 2;
			m_ttf.height = this.height - m_ttf.y - m_skin.buttonBottonMargin;
		}
	}
	/**
	 *
	 */
	override public function setSize(nW:Number, nH:Number):void
	{
		if (m_background)
		{
			m_background.width = nW;
			m_background.height = nH;
		}
	}
}
}